<?php 
class ControllerProductAttribute extends Controller { 
	private $error = array();
   
  	public function index() {
		$this->load->language('product/attribute');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/attribute');
    	$this->getList();
  	}
              
  	public function insert() {
		$this->load->language('product/attribute');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/attribute');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
      		$this->model_product_attribute->addAttribute($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
      		$this->redirect($this->url->link('product/attribute', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getFormText();
    	$this->getInsertForm();
  	}

  	public function update() {
		$this->load->language('product/attribute');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/attribute');
    	if ($this->request->server['REQUEST_METHOD'] == 'POST') {
	  		$this->model_product_attribute->editAttribute($this->request->get['attribute_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/attribute', 'token=' . $this->session->data['token'], 'SSL'));
    	}
		$this->getFormText();
    	$this->getUpdateForm();
  	}
    
  	private function getList() {
  		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_group_name'] = $this->language->get('column_group_name');
		$this->data['column_group_display_name'] = $this->language->get('column_group_display_name');
		$this->data['column_group_id'] = $this->language->get('column_group_id');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_action'] = $this->language->get('column_action');
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/attribute', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('product/attribute/insert', 'token=' . $this->session->data['token'], 'SSL');
		
  		$url = '';
  		if (isset($this->request->get['filter_display_name'])) {
			$filter_display_name = $this->request->get['filter_display_name'];
			$url .= '&filter_display_name=' . $this->request->get['filter_display_name'];
		} else {
			$filter_display_name = '';
		}
  		if (isset($this->request->get['filter_group_display_name'])) {
			$filter_group_display_name = $this->request->get['filter_group_display_name'];
			$url .= '&filter_group_display_name=' . $this->request->get['filter_group_display_name'];
		} else {
			$filter_group_display_name = '';
		}
  		if (isset($this->request->get['filter_group_id'])) {
			$filter_group_id = $this->request->get['filter_group_id'];
			$url .= '&filter_group_id=' . $this->request->get['filter_group_id'];
		} else {
			$filter_group_id = '';
		}
		$url2 = '';
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
			$url2 .= '&sort=' . $this->request->get['sort'];
		} else {
			$sort = 'ad.name';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
			$url2 .= '&order=' . $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['attributes'] = array();
		$data = array(
			'filter_display_name'  => $filter_display_name,
			'filter_group_display_name'  => $filter_group_display_name,
			'filter_group_id'  => $filter_group_id,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$attribute_total = $this->model_product_attribute->getTotalAttributes($data);
		$results = $this->model_product_attribute->getAttributes($data);
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/attribute/update', 'token=' . $this->session->data['token'] . '&attribute_id=' . $result['attribute_id'], 'SSL')
			);
			$this->data['attributes'][] = array(
				'attribute_id'       => $result['attribute_id'],
				'attribute_group_id' => $result['attribute_group_id'],
				'name'               => $result['name'],
				'display_name'       => $result['display_name'],
				'group_name'         => $result['group_name'],
				'group_display_name' => $result['group_display_name'],
				'action'             => $action
			);
		}	

		$url_sort = $url;
		if ($order == 'ASC') {
			$url_sort .= '&order=DESC';
		} else {
			$url_sort .= '&order=ASC';
		}
		if (isset($this->request->get['page'])) {
			$url_sort .= '&page=' . $this->request->get['page'];
		}
		$this->data['sort_display_name'] = $this->url->link('product/attribute', 'token=' . $this->session->data['token'] . '&sort=ad.display_name' . $url_sort, 'SSL');
		$this->data['sort_group_display_name'] = $this->url->link('product/attribute', 'token=' . $this->session->data['token'] . '&sort=group_display_name' . $url_sort, 'SSL');
		
		$pagination = new Pagination();
		$pagination->total = $attribute_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('product/attribute', 'token=' . $this->session->data['token'] . $url . $url2. '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		$this->data['filter_display_name'] = $filter_display_name;
		$this->data['filter_group_display_name'] = $filter_group_display_name;
		$this->data['filter_group_id'] = $filter_group_id;
		$this->data['token'] = $this->session->data['token'];

		$this->template = 'product/attribute_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
  	}
  
  	private function getFormText() {
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_attribute_value_remove_confirm'] = $this->language->get('text_attribute_value_remove_confirm');
		
     	$this->data['heading_title'] = $this->language->get('heading_title');
    	$this->data['entry_name'] = $this->language->get('entry_name');
    	$this->data['entry_display_name'] = $this->language->get('entry_display_name');
		$this->data['entry_attribute_group'] = $this->language->get('entry_attribute_group');
		$this->data['entry_value_name'] = $this->language->get('entry_value_name');
		$this->data['entry_value_display_name'] = $this->language->get('entry_value_display_name');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_remove'] = $this->language->get('button_remove');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_attribute_value'] = $this->language->get('button_add_attribute_value');
    
		$this->data['token'] = $this->session->data['token'];
		$this->data['tab_general'] = $this->language->get('tab_general');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
  		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/attribute', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
  	}
  	
	private function getInsertForm() {
     	$this->data['action'] = $this->url->link('product/attribute/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/attribute', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['name'] = '';
		$this->data['attribute_description'] = array();
		$this->data['attribute_group_id'] = '';
		$this->load->model('product/attribute_group');
		$this->data['attribute_groups'] = $this->model_product_attribute_group->getAttributeGroups();	
		$this->data['attribute_values'] = array();
		
		$this->load->model('tool/image');
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		
		$this->template = 'product/attribute_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);	
		$this->response->setOutput($this->render());	
  	}

	private function getUpdateForm() {
     	$this->data['action'] = $this->url->link('product/attribute/update', 'token=' . $this->session->data['token'] . '&attribute_id=' . $this->request->get['attribute_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/attribute', 'token=' . $this->session->data['token'], 'SSL');

		$attribute_info = $this->model_product_attribute->getAttribute($this->request->get['attribute_id']);
		$this->data['name'] = $attribute_info['name'];
		$this->data['attribute_group_id'] = $attribute_info['attribute_group_id'];
		$this->data['attribute_description'] = $this->model_product_attribute->getAttributeDescriptions($this->request->get['attribute_id']);
		
		$this->load->model('product/attribute_group');
		$this->data['attribute_groups'] = $this->model_product_attribute_group->getAttributeGroups();
		
		$attribute_values = $this->model_product_attribute->getAttributeValueDescriptions($this->request->get['attribute_id']);
		$this->load->model('tool/image');
		$this->data['attribute_values'] = array();
		foreach ($attribute_values as $attribute_value) {
			if ($attribute_value['image'] && file_exists(DIR_IMAGE . $attribute_value['image'])) {
				$image = $attribute_value['image'];
			} else {
				$image = 'no_image.jpg';
			}
			$this->data['attribute_values'][] = array(
				'attribute_value_id'     => $attribute_value['attribute_value_id'],
				'name'                   => $attribute_value['name'],
				'image'                  => $image,
				'thumb'                  => $this->model_tool_image->resize($image, 100, 100),
				'sort_order'             => $attribute_value['sort_order'],
				'attribute_value_description' => $attribute_value['attribute_value_description']
			);
		}
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		
		$this->template = 'product/attribute_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());	
  	}
	
	public function autocomplete() {
		$json = array();
		if (isset($this->request->get['filter_display_name'])) {
			$this->load->model('product/attribute');
			$this->load->model('tool/image');
			$data = array(
				'filter_display_name' => $this->request->get['filter_display_name'],
				'start'       => 0,
				'limit'       => 20
			);
			$attributes = $this->model_product_attribute->getAttributes($data);
			foreach ($attributes as $attribute) {
				$attribute_value_data = array();
				$attribute_values = $this->model_product_attribute->getAttributeValues($attribute['attribute_id']);
				foreach ($attribute_values as $attribute_value) {
					if ($attribute_value['image'] && file_exists(DIR_IMAGE . $attribute_value['image'])) {
						$image = $this->model_tool_image->resize($attribute_value['image'], 50, 50);
					} else {
						$image = '';
					}
					$attribute_value_data[] = array(
						'attribute_value_id' => $attribute_value['attribute_value_id'],
						'name'               => html_entity_decode($attribute_value['name'], ENT_QUOTES, 'UTF-8'),
						'image'              => $image,
						'sort_order'         => $attribute_value['sort_order'],
						'display_name'       => html_entity_decode($attribute_value['display_name'], ENT_QUOTES, 'UTF-8')	
					);
				}
				$json[] = array(
					'attribute_id'       => $attribute['attribute_id'], 
					'name'               => $attribute['name'],
					'display_name'       => $attribute['display_name'],
					'group_name'         => $attribute['group_name'],
					'group_display_name' => $attribute['group_display_name'],
					'attribute_values'   => $attribute_value_data
				);		
			}
		}
		$this->response->setOutput(json_encode($json));
	}		  
}
?>